/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.iecg.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iecg.api.entity.IecgUserDetail;
import com.iecg.service.IecgUserDetailService;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 健康档案
 *
 * @author pigx code generator
 * @date 2019-10-12 16:11:41
 */
@RestController
@AllArgsConstructor
@RequestMapping("/userdetail" )
@Api(value = "iecguserdetail", tags = "健康档案管理")
public class IecgUserDetailController {

    private final IecgUserDetailService iecgUserDetailService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param iecgUserDetail 健康档案
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getIecgUserDetailPage(Page page, IecgUserDetail iecgUserDetail) {
        return R.ok(iecgUserDetailService.page(page, Wrappers.query(iecgUserDetail)));
    }


    /**
     * 通过id查询健康档案
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) String id) {
        return R.ok(iecgUserDetailService.getById(id));
    }

    /**
     * 新增健康档案
     * @param iecgUserDetail 健康档案
     * @return R
     */
    @ApiOperation(value = "新增健康档案", notes = "新增健康档案")
    @SysLog("新增健康档案" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('iecg_userdetail_add')" )
    public R save(@RequestBody IecgUserDetail iecgUserDetail) {
        return R.ok(iecgUserDetailService.save(iecgUserDetail));
    }

    /**
     * 修改健康档案
     * @param iecgUserDetail 健康档案
     * @return R
     */
    @ApiOperation(value = "修改健康档案", notes = "修改健康档案")
    @SysLog("修改健康档案" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('iecg_userdetail_edit')" )
    public R updateById(@RequestBody IecgUserDetail iecgUserDetail) {
        return R.ok(iecgUserDetailService.updateById(iecgUserDetail));
    }

    /**
     * 通过id删除健康档案
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除健康档案", notes = "通过id删除健康档案")
    @SysLog("通过id删除健康档案" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('iecg_userdetail_del')" )
    public R removeById(@PathVariable String id) {
        return R.ok(iecgUserDetailService.removeById(id));
    }


	/**
	 * 通过userId查询健康档案
	 * @param userId userId
	 * @return R
	 */
	@ApiOperation(value = "通过userId查询", notes = "通过userId查询")
	@GetMapping("user_id/{userId}" )
	public R getByUserId(@PathVariable("userId" ) Long userId) {
		return R.ok(iecgUserDetailService.getOne(Wrappers.<IecgUserDetail>query().lambda().eq(IecgUserDetail::getUserId, userId)));
	}

}
